package com.ldy.frame;


import org.json.JSONObject;

import java.io.*;
import java.net.Socket;
import java.util.*;
/**
 * Created by MacBook Pro on 2017/11/22.
 */
public class Client {
    public static final String IP_ADDR = "localhost";//服务器地址192.127.8.127 localhost
    public static final int PORT = 12345;//服务器端口号9001

    public static void main(String[] args) {
        ClientJson();

    }
    public static void ClientJson()
    {
        System.out.println("客户端启动...");
        Socket socket = null;
        DataInputStream input =null;
        DataOutputStream out =null;
        JSONObject json =null;
        String jsonString ="";
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("Name","Bob");
        map1.put("Sex","male");
        map1.put("Age","15");


        Map<String, String> a = TalkServer_test.a;
        while (true)
        {
            try {
                socket = new Socket(IP_ADDR, PORT);
                //生成json对象
                json =new JSONObject(map1);
                //读取服务器端数据

                // input = new DataInputStream(socket.getInputStream());
                //向服务器端发送数据
                jsonString =json.toString();
                byte[] jsonByte =jsonString.getBytes();
                out  = new DataOutputStream(socket.getOutputStream());
                System.out.println("发送数据长度为:"+jsonString.length());
                out.write(jsonByte);
                //写入之后一定要进行flush()操作
                out.flush();
                System.out.println("数据传输完毕!");
            }catch (IOException e)
            {
                System.out.println("客户端异常："+e.getMessage());
                break;
            }finally {
                if(socket!=null)
                    try{
                        socket.close();
                    }catch (IOException e)
                    {
                        socket =null;
                        System.out.println("客户端finally异常"+e.getMessage());
                    }
                break;
            }

        }

    }
}
